package com.irain.pp.tool.email;

import com.irain.pp.tool.ExceptionUtils;
import org.apache.commons.mail.*;

import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;

/**
 * @author iRain
 * @date 2017/12/11 14:24
 */
public class EmailAdapter {

    private final String hostName;
    private final String userName;
    private final String password;
    private final String fromAddress;
    private final String fromName;

    public EmailAdapter(String hostName, String userName, String password, String fromAddress, String fromName) {
        this.hostName = hostName;
        this.userName = userName;
        this.password = password;
        this.fromAddress = fromAddress;
        this.fromName = fromName;
    }

    public void sendSimpleEmail(String toAddress, String toName){
        try {
            SimpleEmail email = new SimpleEmail();
            email.setHostName(hostName);
            email.setAuthentication(userName, password);
            email.setCharset("utf-8");
            email.setFrom(fromAddress, fromName, "utf-8");
            email.addTo(toAddress, toName, "utf-8");
            //email.addCc(xxx, xxx);
            //email.addBcc(xxx, xxx);
            email.setSubject("测试主题");
            email.setMsg("这是一个简单的测试！");
            email.send();
        } catch (EmailException e){
            throw ExceptionUtils.transfer(e);
        }
    }

    // 发送Html内容的邮件
    public void sendHtmlEmail(String toAddress, String toName){
        try {
            HtmlEmail email = new HtmlEmail();
            email.setHostName(hostName);
            email.setAuthentication(userName, password);
            email.setCharset("utf-8");
            email.addTo(toAddress, toName, "utf-8");
            //email.addCc(xxx, xxx);//设置抄送
            //email.addBcc(xxx, xxx);//设置密送
            email.setFrom(fromAddress, fromName, "utf-8");
            email.setSubject("这是一个html邮件");
            // 设置html内容，实际使用时可以从文本读入写好的html代码
            email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>");
            email.send();
        }catch (EmailException e){
            throw ExceptionUtils.transfer(e);
        }

    }

    // 发送复杂的邮件，包含附件等
    public void sendMultiPartEmail(String toAddress, String toName){
        try {
            MultiPartEmail email = null;
            email = new MultiPartEmail();
            email.setHostName(hostName);
            email.setAuthentication(userName, password);
            email.setCharset("utf-8");
            email.addTo(toAddress, toName, "utf-8");
            //email.addCc(xxx, xxx);//设置抄送
            //email.addBcc(xxx, xxx);//设置密送
            email.setFrom(fromAddress, fromName, "utf-8");
            email.setSubject("这是有附件的邮件");
            email.setMsg("<a href='#'>测试内容</a>");

            // 为邮件添加附加内容
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("D:\\邮件.txt");// 本地文件
            // attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("描述信息");
            // 设置附件显示名字，必须要编码，不然中文会乱码
            attachment.setName(MimeUtility.encodeText("邮件.txt"));
            // 将附件添加到邮件中
            email.attach(attachment);
            email.send();
        } catch (EmailException | UnsupportedEncodingException e){
            throw ExceptionUtils.transfer(e);
        }
    }


    public static void main(String[] args) throws Exception{
        EmailAdapter emailAdapter = new EmailAdapter("smtp.163.com","sunirain@163.com",
                "330283wypxhyy","sunirian@163.com","pp 官方");
        emailAdapter.sendHtmlEmail("97099124@qq.com","97099124@qq.com");
        System.out.println("发送成功");
    }
}
